<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.cqggrw.domain.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>公共人物网后台管理</title>
<link type="text/css" rel="Stylesheet" href="${pageContext.request.contextPath}/Admin/Content/tabbed_pages.css" />
<link type="text/css" rel="Stylesheet" href="${pageContext.request.contextPath}/Admin/Content/Index.css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/jslib/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
	function WebForm_OnSubmit() {
		$.ajax({
			url : '${pageContext.request.contextPath}/touPiaoAction!modify.action',
			data : {
				id : $('#objectName').attr('name'),
				name : $('#objectName').val()
			},
			async : false,
			type : 'POST',
			dataType : 'json',
			success : function(result) {
			}
		});

		var opts = $("input[name='option']");
		for ( var i = 0; i < opts.length; i++) {
			if (opts[i].id != '') {
				$.ajax({
					url : '${pageContext.request.contextPath}/touPiaoAction!modifyOpt.action',
					data : {
						objId : opts[i].id,
						name : opts[i].value
					},
					async : false,
					type : 'POST',
					dataType : 'json',
					success : function(result) {
					}
				});
			} else {
				$.ajax({
					url : '${pageContext.request.contextPath}/touPiaoAction!addOpt.action',
					data : {
						id : $('#objectName').attr('name'),
						name : opts[i].value
					},
					async : false,
					type : 'POST',
					dataType : 'json',
					success : function(result) {
					}
				});
			}
		}
		$('#msgInfo').text("修改成功！");
		$('#toupiaoTable').css("display", "none");
		$('#heheh').css("display", "none");
		$('#backId').css("display", "block");
		return false;
	}
</script>
</head>
<body>
	<%
		TPiao tp = (TPiao)request.getAttribute("tp");
	%>
	<form name="form1" method="post" action="touPiaoAction!add.action" onsubmit="javascript:return WebForm_OnSubmit();" id="form1" enctype="multipart/form-data">
		<div class="form-div textLeft">
			<h1>
				<span class="action-span"><a href="Admin/toupiao/tpList.jsp">投票列表</a> </span> <span class="action-span1 fs20">公共人物网 管理中心 </span><span class="action-span1 paddingTop5"> - 分类编辑 </span>
			</h1>
			<div style="clear:both"></div>
		</div>
		<div id="gallery">
			<div class="on" title="CommonInfo">
				<span>分类信息</span>
			</div>
			<span id="msgInfo"></span>
			<%
				String message = (String) request.getAttribute("message");
						if (message != null) {
							out.print(message);
						}
			%>
		</div>
			<div id="CommonInfo" class="show">
				<table id="toupiaoTable" width="90%" id="general-table" align="center">
					<tr>
						<td>投票项目： <input name="<%=tp.getId()%>" style="width: 300px;" type="text" id="objectName" value="<%=tp.getName()%>" /> <span id="nameSpan"></span>
						</td>
					</tr>
					<%
						int i = 0;
										for(TPiaoOpt tpo : tp.getTp()) {
											if(i == 0) {
					%>
					<tr>
						<td>投票选项： <input style="width: 350px;" id="<%=tpo.getId()%>" value="<%=tpo.getName()%>" name="option" type="text" id="optId" /></td>
					</tr>
					<%
						} else {
					%>
					<tr>
						<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input id="<%=tpo.getId()%>" value="<%=tpo.getName()%>" style="width: 350px;" name="option" type="text" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" name="<%=tpo.getId()%>" onclick="delOption(this);" value="删除选项" /></td>
					</tr>
					<%
						}
										i++;
										}
					%>
					<tr>
						<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="button" onclick="addOption();" value="增加选项" /></td>
					</tr>
				</table>
			</div>
			<div id="heheh" class="form-div textLeft" style="text-align:center;margin-top:5px;">
				<input type="submit" name="btnSave" value="保存" onclick="" id="btnSave" class="button" />
			</div>
	</form>
	<div id="backId" class="form-div textLeft" style="text-align:center;margin-top:5px;display: none;">
		<a href="Admin/toupiao/tpList.jsp"><input type="button" name="btnSave" value="返回" onclick="" id="btnSave" class="button" />
		</a>
	</div>
	<script type="text/javascript">
		var addOption = function() {
			var str = '<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + '<input style="width: 350px;" name="option" type="text"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" onclick="delOption(this);" value="删除选项" /></td></tr>';
			$('#toupiaoTable tr:last-child').before(str);
		};

		var delOption = function(obj) {
			if ($(obj).attr('name') != '' && confirm("是否删除该投票选项？")) {
				$.ajax({
					url : '${pageContext.request.contextPath}/touPiaoAction!del.action',
					data : {
						id : $(obj).attr('name')
					},
					dataType : 'json'
				});
			} 
				$(obj).parent().parent().remove();
		};
	</script>
</body>
</html>